<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 组件-更换抬头弹窗 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="component-modal-title" @click.stop>
		<uni-popup ref="popupModal" type="bottom" :safe-area="false" @change="onChange">
			<view class="popup-box" :style="{'--theme-color': themeColor}">
				<view class="popup-header">
					<view class="title">更换抬头</view>
					<image class="close" src="/static/close.png" mode="aspectFit" @click="onClose"></image>
				</view>
				<view class="popup-content">
					<scroll-view scroll-y class="content-list" @scrolltolower="onBottom" v-if="titleList.length">
						<view class="list-item" :class="{select: item.id == selectTitle}" v-for="item in titleList" :key="item.id" @click="onConfirm(item)">
							<view class="item-head">
								<text v-if="item.invoice_type == 1">普通发票 抬头-{{item.title_type == 1 ? "个人" : "单位"}}</text>
								<text v-else-if="item.invoice_type == 2">专用发票</text>
							</view>
							<view class="item-info">
								<view class="info-title text-ellipsis">{{item.invoice_title}}</view>
								<view class="info-subtitle text-ellipsis">{{item.mobile}}</view>
							</view>
							<view class="item-select" v-if="item.id == selectTitle">
								<image class="icon" src="/static/tick.png" mode="aspectFit"></image>
							</view>
						</view>
					</scroll-view>
					<empty top="30%" title="暂无相关内容~" v-else></empty>
				</view>
				<view class="popup-footer">
					<view class="footer-btn" @click="toAddTitle()">添加抬头</view>
					<view class="safe-padding"></view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		name: "componentModalTitle",
		data() {
			return {
				// 抬头列表
				titleList: [],
				// 分类查询参数
				page: 1,
				limit: 10,
				hasMore: false,
				// 已选抬头
				selectTitle: null,
			};
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		methods: {
			// 获取抬头列表
			getTitleList() {
				this.$util.request("passenger.invoice.title.list", {
					page: this.page,
					limit: this.limit,
				}).then(res => {
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.count / this.limit ? true : false
						this.titleList = this.page == 1 ? list : [...this.titleList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取抬头列表 ', error)
				})
			},
			// 打开模态框
			open(value) {
				if (value) {
					this.selectTitle = value
				} else {
					this.selectTitle = null
				}
				this.page = 1
				this.getTitleList()
				this.$refs.popupModal.open()
			},
			// 关闭弹窗
			onClose() {
				this.$refs.popupModal.close()
			},
			// 确认
			onConfirm(item) {
				this.$emit("confirm", item)
				this.onClose()
			},
			// 改变事件
			onChange(e) {
				this.$emit("onChange", e.show)
			},
			// 添加抬头
			toAddTitle() {
				this.$util.toPage({
					mode: 1,
					path: '/pages/passenger/mine/invoice/addTitle',
					isToken: true,
				})
				this.onClose()
			},
			// 页面滚动底部
			onBottom() {
				if (this.hasMore) {
					this.page++
					this.getTitleList()
				}
			},
		},
	}
</script>

<style lang="scss">
	.component-modal-title {
		position: relative;
		z-index: 999;

		.popup-box {
			background: #F6F7FB;
			border-radius: 16rpx 16rpx 0 0;

			.popup-header {
				display: flex;
				align-items: center;
				padding: 32rpx 32rpx 0 64rpx;

				.title {
					color: #242629;
					text-align: center;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 44rpx;
					flex: 1;
				}

				.close {
					width: 32rpx;
					height: 32rpx;
				}
			}

			.popup-content {
				padding: 32rpx;
				height: 65vh;

				.content-list {
					height: 100%;

					.list-item {
						margin-top: 32rpx;
						background: #fff;
						border-radius: 16rpx;
						border: 4rpx solid transparent;
						position: relative;

						&:first-child {
							margin-top: 0;
						}

						.item-head {
							color: #242629;
							font-size: 28rpx;
							line-height: 40rpx;
							padding: 20rpx 32rpx;
							border-bottom: 1px solid #F6F7FB;
							margin: -4rpx -4rpx 0;
						}

						.item-info {
							padding: 32rpx 48rpx 32rpx 32rpx;
							margin: 0 -4rpx -4rpx;

							.info-title {
								color: #242629;
								font-size: 28rpx;
								font-weight: 600;
								line-height: 40rpx;
							}

							.info-subtitle {
								margin-top: 16rpx;
								color: #ACADB7;
								font-size: 28rpx;
								line-height: 40rpx;
							}
						}

						.item-select {
							width: 32rpx;
							height: 32rpx;
							border-radius: 50%;
							position: absolute;
							top: -4rpx;
							right: -4rpx;
							background: var(--theme-color);
						}

						&.select {
							border-color: var(--theme-color);
						}
					}
				}
			}

			.popup-footer {
				padding: 12rpx 32rpx;
				background: #ffffff;

				.footer-btn {
					color: #ffffff;
					font-size: 32rpx;
					line-height: 44rpx;
					padding: 22rpx 24rpx;
					border-radius: 40rpx;
					text-align: center;
					background: var(--theme-color);
				}
			}
		}
	}
</style>